﻿using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using System;

namespace SharedClassLibrary.HttpRequest
{
    public static class ApiClientServiceCollectionExtensions
    {
        public static void AddApiClient(this IServiceCollection services, Action<ApiClientOptions> configureOptions)
        {
            services.Configure<ApiClientOptions>(configureOptions);
            services.AddSingleton(provider =>
            {
                return provider.GetRequiredService<IOptions<ApiClientOptions>>().Value;
            });
            services.AddTransient<ApiClientLoggingDelegatingHandler>();
            services.AddScoped<ApiClientJwtDelegatingHandler>();
            services.AddHttpClient<ApiClient>()
                    .AddHttpMessageHandler<ApiClientLoggingDelegatingHandler>();
                    //.AddHttpMessageHandler<ApiClientJwtDelegatingHandler>();
        }
    }
}
